package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.Activity.CoalMoneyActivity;
import com.tianxin.workbao.workBao.Activity.CoalMoneyCalcActivity;
import com.tianxin.workbao.workBao.Activity.ContractActivity;
import com.tianxin.workbao.workBao.Activity.ContractCoalActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Contract;
import com.tianxin.workbao.workBao.bean.CtCoaltype;
import com.tianxin.workbao.workBao.bean.VContract;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.ContractProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class ContractAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<VContract> mVContracts;
    private Context mContext;

    public ContractAdapter(List<VContract> list, Context context) {
        this.mVContracts = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mVContracts.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contract_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mVContracts.get(position));
            viewHolder.SetViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mVContracts.size() > 0) {
            return mVContracts.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIv;
        TextView numberTv;
        TextView timeTv;
        TextView companyTv;
        ExpandView expandView;
        TextView statusLabelTv;
        TextView infoTv;
        TextView payableTv;
        TextView alreadyTv;
        TextView surplusTv;
        TextView coalInformationTv;
        TextView prePayableTv;
        TextView preSurplusTv;
        Button oauthBtn;
        Button settleRecordBtn;
        Button payRecordBtn;
        Button coalListBtn;


        public ViewHolder(View itemView) {
            super(itemView);
            arrowIv = (ImageView) itemView.findViewById(R.id.arrow);
            numberTv = (TextView) itemView.findViewById(R.id.number);
            timeTv = (TextView) itemView.findViewById(R.id.time);
            companyTv = (TextView) itemView.findViewById(R.id.company);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            oauthBtn = (Button) itemView.findViewById(R.id.oauth_btn);
            statusLabelTv = (TextView) itemView.findViewById(R.id.status_label);
            infoTv = (TextView) itemView.findViewById(R.id.info);
            payableTv = (TextView) itemView.findViewById(R.id.payable);
            alreadyTv = (TextView) itemView.findViewById(R.id.already);
            surplusTv = (TextView) itemView.findViewById(R.id.surplus);
            coalInformationTv = (TextView) itemView.findViewById(R.id.coal_information);
            prePayableTv = (TextView) itemView.findViewById(R.id.pre_payable);
            preSurplusTv = (TextView) itemView.findViewById(R.id.pre_surplus);
            settleRecordBtn = (Button) itemView.findViewById(R.id.settle_record);
            payRecordBtn = (Button) itemView.findViewById(R.id.pay_record);
            coalListBtn = (Button) itemView.findViewById(R.id.coal_list_btn);
        }

        public void BindViewData(VContract vContract) {
            numberTv.setText(vContract.getContract().getCtnum());
            if (vContract.getContract().getCttype() == Contract.CONTRACT_CPTYPE_IN) {
                companyTv.setText(vContract.getContract().getSalername());
            } else {
                companyTv.setText(vContract.getContract().getBuyername());
            }
            String info;
            info = vContract.getContract().getSalecontroltype() == 1 ? "控制发货 " : "不控制发货 ";
            info += vContract.getContract().getIstestrap() == 1 ? "考核指标 " : "不考核指标 ";
            info += vContract.getContract().getTranspaytype() == 1 ? "我方付运费 " : "对方付运费 ";
            infoTv.setText(info);
            if (vContract.getContract().getCttype() == Contract.CONTRACT_CPTYPE_OUT) {
                payableTv.setText("应收" + vContract.getContract().getReceivablemoney());
                alreadyTv.setText("已收" + vContract.getContract().getTotalreceivemoney());
                surplusTv.setText("未收" + vContract.getContract().getSurplusmoney());
            } else {
                payableTv.setText("应付" + vContract.getContract().getPayablemoney());
                alreadyTv.setText("已付" + vContract.getContract().getTotalpaymoney());
                surplusTv.setText("未付" + vContract.getContract().getSurplusmoney());
            }
            if (vContract.getPreSettlement() != null) {
                if (vContract.getContract().getCttype() == Contract.CONTRACT_CPTYPE_OUT) {
                    prePayableTv.setText("预结应收" + vContract.getPreSettlement().getReceivablemoney());
                    preSurplusTv.setText("预结未收" + vContract.getPreSettlement().getPremoney());
                } else {
                    prePayableTv.setText("预结应付" + vContract.getPreSettlement().getPrepayablemoney());
                    preSurplusTv.setText("预结余额" + vContract.getPreSettlement().getPremoney());
                }
            } else {
                prePayableTv.setVisibility(View.GONE);
                preSurplusTv.setVisibility(View.GONE);
            }
            String coalInfo = "";
            List<CtCoaltype> coalList = vContract.getCoalTypeList();
            if (coalList == null || coalList.isEmpty()) {
                coalInformationTv.setVisibility(View.GONE);
            } else {
                for (int i = 0; i < coalList.size(); i++) {
                    CtCoaltype ctCoaltype = coalList.get(i);
                    if (coalInfo.length() == 0) {
                        coalInfo = ctCoaltype.getCoaltypename()
                                + " 单价" + ctCoaltype.getContractunitprice()
                                + " 签订" + ctCoaltype.getSignweight() + "吨"
                                + " 已运" + ctCoaltype.getAlreadysetweight() + "吨"
                                + " 剩余" + ctCoaltype.getNosetweight() + "吨";
                    } else {
                        coalInfo += "\n" + ctCoaltype.getCoaltypename()
                                + " 单价" + ctCoaltype.getContractunitprice()
                                + " 签订" + ctCoaltype.getSignweight() + "吨"
                                + " 已运" + ctCoaltype.getAlreadysetweight() + "吨"
                                + " 剩余" + ctCoaltype.getNosetweight() + "吨";
                    }
                }
                coalInformationTv.setText(coalInfo);
            }
            switch (vContract.getContract().getStatus()) {
                case 0:
                    statusLabelTv.setText("未审");
                    timeTv.setText(TimeUtils.parseDateTime(vContract.getContract().getAddtime()));
                    oauthBtn.setVisibility(View.VISIBLE);
                    payRecordBtn.setVisibility(View.GONE);
                    settleRecordBtn.setVisibility(View.GONE);
                    break;
                case 2:
                    statusLabelTv.setText("已审");
                    timeTv.setText(TimeUtils.parseDateTime(vContract.getContract().getSigndate()));
                    oauthBtn.setVisibility(View.GONE);
                    break;
                case 3:
                    statusLabelTv.setText("完毕");
                    timeTv.setText(TimeUtils.parseDateTime(vContract.getContract().getSigndate()));
                    oauthBtn.setVisibility(View.GONE);
                    break;
                case 4:
                    statusLabelTv.setText("作废");
                    timeTv.setText(TimeUtils.parseDateTime(vContract.getContract().getSigndate()));
                    oauthBtn.setVisibility(View.GONE);
                    break;
            }
        }

        public void SetViewListener(final int position) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Contract contract;
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;
                        case R.id.oauth_btn:
                            contract = mVContracts.get(position).getContract();
                            contract.setAudituserid((int) SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0));
                            contract.setAuditusername(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "no"));
                            contract.setStatus(2);
                            expandView.setVisibility(View.GONE);
                            getContractProvide().oauth(contract);
                            break;
                        case R.id.pay_record:
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("contract", mVContracts.get(position).getContract());
                            Intent intent = new Intent(mContext, CoalMoneyActivity.class);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                            break;
                        case R.id.settle_record:
                            Bundle bundle1 = new Bundle();
                            bundle1.putSerializable("contract", mVContracts.get(position).getContract());
                            Intent intent1 = new Intent(mContext, CoalMoneyCalcActivity.class);
                            intent1.putExtras(bundle1);
                            mContext.startActivity(intent1);
                            break;
                        case R.id.coal_list_btn:
                            Bundle bundle3 = new Bundle();
                            bundle3.putSerializable("contract", mVContracts.get(position).getContract());
                            Intent intent3 = new Intent(mContext, ContractCoalActivity.class);
                            intent3.putExtras(bundle3);
                            mContext.startActivity(intent3);
                            break;
                    }
                }
            };
            arrowIv.setOnClickListener(listener);
            oauthBtn.setOnClickListener(listener);
            payRecordBtn.setOnClickListener(listener);
            settleRecordBtn.setOnClickListener(listener);
            coalListBtn.setOnClickListener(listener);
        }
    }

    private int mPosition;
    private UpdateListViewListener listener = new UpdateListViewListener() {
        @Override
        public void onRefreshData(Object result) {

        }

        @Override
        public void onLoadMoreData(Object result) {

        }

        @Override
        public void showMessage(String msg) {
            if (msg == "ok") {
                mVContracts.get(mPosition).getContract().setStatus(2);
                notifyDataSetChanged();
                Toast.makeText(mContext, "操作成功", Toast.LENGTH_LONG).show();
            } else if (msg.contains("failure")) {
                Toast.makeText(mContext, "操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void showProgress() {
            ContractActivity activity = (ContractActivity) mContext;
            activity.getSweetAlertDialog(activity).showDialog("", 1200);
        }

        @Override
        public void hideProgress() {
            ContractActivity activity = (ContractActivity) mContext;
            activity.getSweetAlertDialog(activity).dismiss();
        }
    };
    private ContractProvide provide;

    public ContractProvide getContractProvide() {
        if (provide == null) {
            provide = new ContractProvide(mContext, listener);
        }
        return provide;
    }

}
